﻿using famails.common;
using famails.dto;
using famails.bus;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Script.Services;
using System.Text.RegularExpressions;
using System.Text;
using System.Web.Services;
using famails.App_Code.source.bus;

namespace famails.BackEnd.Customer
{
    public partial class CustomerCareAutoResult : System.Web.UI.Page
    {
        MailGroupBUS mailGroupBus = null;
        EventDetailBUS eventDetailBus = null;
        EventBUS eventBus = null;
        DataTable group = null;
        log4net.ILog logs = log4net.LogManager.GetLogger("ErrorRollingLogFileAppender");
        UserLoginDTO userLogin = null;
        CM_CustomerCareAutoBUS cseBus = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            userLogin = getUserLogin();
            if (!IsPostBack)
            {
                try
                {
                    LoadContentList();
                }
                catch (Exception)
                {
                }
            }

        }
        private void LoadContentList()
        {
            try
            {
                object obj = Request.QueryString["id"];
                int Id = 0;
                if (int.TryParse(obj + "", out Id))
                {
                    LoadReport(Id);
                }

            }
            catch (Exception ex)
            {
                logs.Error(userLogin.Username + "-Create-Event - LoadContentList", ex);
            }
        }

        void LoadReport(int Id)
        {
            try
            {
                cseBus = new CM_CustomerCareAutoBUS();
                DataTable tblContent = cseBus.GetReportById(Id, userLogin.UserId);
                dlContentSendEvent.DataSource = tblContent;
                dlContentSendEvent.DataBind();
                
                for (int i = 0; i < tblContent.Rows.Count; i++)
                {
                    Label lblChart = (Label)dlContentSendEvent.Items[i].FindControl("lblChart");
                    Label lblNotOpen2 = (Label)dlContentSendEvent.Items[i].FindControl("lblNotOpen2");

                    int total = 0, err = 0, open, click, notreceve, recieve, bounce;
                    int.TryParse(tblContent.Rows[i]["TotalSend"] + "", out total);
                    int.TryParse(tblContent.Rows[i]["TotalErr"] + "", out err);
                    int.TryParse(tblContent.Rows[i]["TotalOpen"] + "", out open);
                    int.TryParse(tblContent.Rows[i]["TotalClick"] + "", out click);
                    int.TryParse(tblContent.Rows[i]["TotalNotRecieve"] + "", out notreceve);
                    int.TryParse(tblContent.Rows[i]["TotalBounce"] + "", out bounce);
                    int.TryParse(tblContent.Rows[i]["TotalRecieve"] + "", out recieve);

                    //lblNotOpen2.Text = tblContent.Rows[0]["TotalNotOpen"] + "";

                    if (total == 0) lblChart.Text = "Chưa được gửi";
                    else
                        CreateChart(total, err + bounce, open, 1, notreceve, tblContent.Rows[i]["Subject"] + "", lblChart, tblContent.Rows[i]["id"]);
                    //CreateChart(Convert.ToInt32(tblContent.Rows[i]["TotalSend"] + ""), Convert.ToInt32(tblContent.Rows[i]["TotalErr"] + ""), Convert.ToInt32(tblContent.Rows[i]["TotalOpen"] + ""), Convert.ToInt32(tblContent.Rows[i]["TotalRecieve"] + ""), Convert.ToInt32(tblContent.Rows[i]["TotalNotRecieve"] + ""), tblContent.Rows[i]["Subject"] + "", lblChart);
                }
            }
            catch (Exception ex)
            {
                logs.Error("LoadReport", ex);
            }
        }
        private UserLoginDTO getUserLogin()
        {
            if (Session["us-login"] != null)
            {
                return (UserLoginDTO)Session["us-login"];
            }
            else Response.Redirect("~");
            return null;
        }


        private void CreateChart(int total, int err, int open, int NotOpen, int notRecive, string campainName, Label lblChart, object id)
        {
            if (total == 0) total = 1;
            float percentErr = 1;
            percentErr = (err * 100) / total;
            double percentSend = 100 - percentErr;
            float percentOpen = 1;
            percentOpen = (open * 100) / total;
            double percentNotOpen = percentSend - percentOpen;
            double percentNotRecev = (notRecive * 100) / total;

            string html = "<div class='col-xs-12 col-sm-12 col-md-12 col-lg-12' style='padding:0px;border:1px solid #dedede;'>" +
                       "<div class='col-xs-12 col-sm-12 col-md-12 col-lg-12' style='font-size:16px;line-height:40px;height:40px;color:#333;font-weight:bold;text-align:center;background:#f9f9f9;'>" +
                            "Biểu đồ thống kê" +
                       "</div>" +
                       "<div class='col-xs-12 col-sm-12 col-md-12 col-lg-12' style='padding:10px 10px 0px;'>" +
                        "<div class='col-xs-12 col-sm-12 col-md-12 col-lg-12' style='background:#fff;color:#fff;padding:0px;'>" +
                            "<div class='col-xs-5 col-sm-5 col-md-3 col-lg-3' style='text-align:left;padding:0px 5px 0px 0px;color:#333;font-size:12px;line-height:35px;'>" +
                                "Tổng số email gửi :" +
                            "</div>" +
                            "<div class='col-xs-7 col-sm-7 col-md-9 col-lg-9' style='padding:2px 0px 2px 5px;border-radius:0px;'>" +
                                "<div class='progress-bar' style='background:#109618;height:35px;line-height:35px;width: " + percentSend.ToString() + "%;border-radius:0px;'>" +
                                    percentSend.ToString() + "%" +
                                "</div>" +
                            "</div>" +
                       "</div>" +
                       "<div class='col-xs-12 col-sm-12 col-md-12 col-lg-12' style='background:#fff;color:#fff;padding:0px;'>" +
                            "<div class='col-xs-5 col-sm-5 col-md-3 col-lg-3' style='text-align:left;padding:0px 5px 0px 0px;color:#333;font-size:12px;line-height:35px;'>" +
                                "Email lỗi :" +
                            "</div>" +
                            "<div class='col-xs-7 col-sm-7 col-md-9 col-lg-9' style='padding:2px 0px 2px 5px;border-radius:0px;'>" +
                                "<div class='progress-bar' style='background:#dc3912;height:35px;line-height:35px;width:" + percentErr.ToString() + "%;border-radius:0px;'>" +
                                    percentErr.ToString() + "%" +
                                "</div>" +
                            "</div>" +
                       "</div>" +
                       "<div class='col-xs-12 col-sm-12 col-md-12 col-lg-12' style='background:#fff;color:#fff;padding:0px;'>" +
                            "<div class='col-xs-5 col-sm-5 col-md-3 col-lg-3' style='text-align:left;padding:0px 5px 0px 0px;color:#333;font-size:12px;line-height:35px;'>" +
                                "Email đã mở :" +
                            "</div>" +
                            "<div class='col-xs-7 col-sm-7 col-md-9 col-lg-9' style='padding:2px 0px 2px 5px;border-radius:0px;'>" +
                                "<div class='progress-bar' style='background:#3366CC;height:35px;line-height:35px;width:" + percentOpen.ToString() + "%;border-radius:0px;'>" +
                                    percentOpen.ToString() + "%" +
                                "</div>" +
                            "</div>" +
                       "</div>" +
                       "<div class='col-xs-12 col-sm-12 col-md-12 col-lg-12' style='background:#fff;color:#fff;padding:0px;'>" +
                            "<div class='col-xs-5 col-sm-5 col-md-3 col-lg-3' style='text-align:left;padding:0px 5px 0px 0px;color:#333;font-size:12px;line-height:35px;'>" +
                                "Email chưa mở :" +
                            "</div>" +
                            "<div class='col-xs-7 col-sm-7 col-md-9 col-lg-9' style='padding:2px 0px 2px 5px;border-radius:0px;'>" +
                                "<div class='progress-bar' style='background:#FF9900;height:35px;line-height:35px;width:" + percentNotOpen.ToString() + "%;border-radius:0px;'>" +
                                    percentNotOpen.ToString() + "%" +
                                "</div>" +
                            "</div>" +
                       "</div>" +
                       "<div class='col-xs-12 col-sm-12 col-md-12 col-lg-12' style='background:#fff;color:#fff;padding:0px;'>" +
                            "<div class='col-xs-5 col-sm-5 col-md-3 col-lg-3' style='text-align:left;padding:0px 5px 0px 0px;color:#333;font-size:12px;line-height:35px;'>" +
                                "Từ chối nhận tin :" +
                            "</div>" +
                            "<div class='col-xs-7 col-sm-7 col-md-9 col-lg-9' style='padding:2px 0px 2px 5px;border-radius:0px;'>" +
                                "<div class='progress-bar' style='background:#0099C6;height:35px;line-height:35px;width:" + percentNotRecev.ToString() + "%;border-radius:0px;'>" +
                                    percentNotRecev.ToString() + "%" +
                                "</div>" +
                            "</div>" +
                       "</div>" +
                       "<div class='col-xs-12 col-sm-12 col-md-12 col-lg-12' style='background:#fff;color:#fff;padding:0px;'>" +
                            "<div class='col-xs-5 col-sm-5 col-md-3 col-lg-3' style='text-align:left;padding:0px 5px 0px 0px;color:#333;font-size:12px;line-height:35px;'>" +
                                "Chi tiết :"+
                            "</div>" +
                            "<div class='col-xs-7 col-sm-7 col-md-9 col-lg-9' style='padding:2px 0px 2px 5px;border-radius:0px;line-height:30px;'>" +
                                    "<a href='openlist.aspx?id="+id+"' target='_blank' style='font-weight:bold;color:#333;'>Danh sách email mở</a>" +
                            "</div>" +
                       "</div>" +
                       "</div>" +
  "</div>";
            lblChart.Text = html;
        }
        public DataTable GetOpenMailByContentID(object contentid)
        {
            return new EventBUS().GetOpenMailByContentID(contentid);
        }

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static List<SendMailDTO> ErrorMailList(decimal id)
        {
            //string name = "", phone = "h", email = ""; int group = -1;
            EventBUS ctBUS = new EventBUS();
            DataTable dt = ctBUS.GetErrorMailList(id, 1);
            List<SendMailDTO> result = new List<SendMailDTO>();
            SendMailDTO item = new SendMailDTO();
            foreach (DataRow dr in dt.Rows)
            {
                item = new SendMailDTO();
                item.ID = Convert.ToDecimal(dr["id"] + "");
                item.To = dr["TO"] + "";
                item.Message = dr["MessageId"] + "";
                item.Status = dr["Status"] + "";
                if (dr["SendTime"] + "" != "")
                    item.SendTime = Convert.ToDateTime(dr["SendTime"] + "");
                result.Add(item);
            }

            return result;
        }
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static List<EventDetailDTO> Search(int eventID, string from, string to)
        {
            EventDetailBUS eventDetailBus = new EventDetailBUS();
            DataTable customerBySelect = eventDetailBus.GetByGroupIdNew(0, eventID, from, to);
            List<EventDetailDTO> result = new List<EventDetailDTO>();
            EventDetailDTO item = new EventDetailDTO();
            foreach (DataRow dr in customerBySelect.Rows)
            {
                
                item = new EventDetailDTO();
                item.EventId = Convert.ToInt32(dr["EventId"] + "");
                item.GroupId = Convert.ToInt32(dr["GroupId"] + "");
                item.FullName = dr["FullName"] + "";
                item.EmailID = dr["EmailId"] + "";
                item.Job = dr["Job"] + "";
                item.Company = dr["Company"] + "";
                item.Sex = dr["sex"] + "";
                item.CreateDate = Convert.ToDateTime(dr["CreateDate"] + "");
                item.Phone = dr["phone"] + "";
                item.Address = dr["address"] + "";
                result.Add(item);
            }

            return result;
        }
    }
    public class Result { public string OpenEmail { get; set; } public DateTime OpenDate { get; set; } }
}